package com.qf.controller;

import com.qf.pojo.Cart;
import com.qf.service.CartService;
import com.qf.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @RequestMapping("/select/{id}")
    @ResponseBody
    public Msg<Cart> select(@PathVariable("id") int u_id){
        List<Cart> selectall = cartService.selectall(u_id);
        System.out.println("selectall = " + selectall);
        System.out.println(selectall);
        Msg<Cart> cartMsg = new Msg<>();
        //cartMsg.setMsg(selectall);;
        return cartMsg ;
    }
   @RequestMapping("/delete")
   @ResponseBody
    public Msg<Cart> delete(Integer id){
        int delete = cartService.delete(1);
       Msg<Cart> cartMsg = new Msg<>();
       cartMsg.setMsg("删除成功");
       cartMsg.setCode(delete);
       return cartMsg;

    }
   @RequestMapping("/update")
   @ResponseBody
    public Msg<Cart> update(Cart a) {
       Cart cart = new Cart(2,2,3,5,999,"false");
       int update = cartService.update(cart);
       Msg<Cart> cartMsg = new Msg<>();
       cartMsg.setMsg("更新成功");
       cartMsg.setCode(1);

       return cartMsg;
    }

    @RequestMapping("/selectone/{id}")
    @ResponseBody
    public String selectone(@PathVariable("id") int id){
        Cart cart = cartService.selectone(id);
        System.out.println(cart.toString());
        return cart.toString();
    }
    @RequestMapping("/add")
    @ResponseBody
    public Msg<Cart> add(Cart a){

        int add = cartService.add(a);
        System.out.println(add);
        Msg<Cart> cartMsg = new Msg<>();
        cartMsg.setMsg("添加成功");
        cartMsg.setCode(1);
        return cartMsg;
    }

    @RequestMapping("/numadd")
    @ResponseBody
    public Msg<Cart> numadd( ){
        Cart selectone = cartService.selectone(3);
        selectone.setCart_num(selectone.getCart_num()+1);
        int update = cartService.update(selectone);
        Msg<Cart> cartMsg = new Msg<>();
        cartMsg.setMsg("增加成功");
        cartMsg.setCode(1);
        return cartMsg;
    }
   @RequestMapping("/numsub/{id}")
   @ResponseBody
    public Msg<Cart> numsub(@PathVariable("id") int cart_id){
        Cart selectone = cartService.selectone(cart_id);
        if(selectone.getCart_num()==1){
            int delete = cartService.delete(cart_id);
            System.out.println("delete = " + delete);
        }
        selectone.setCart_num(selectone.getCart_num()-1);
        int update = cartService.update(selectone);
       Msg<Cart> cartMsg = new Msg<>();
       cartMsg.setMsg("修改成功");
       cartMsg.setCode(1);
       return cartMsg;
    }

 }

